﻿using App.Model;
using App.Untiys;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Win.Dal;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace App.Desk.Sys.Pails
{
    public partial class FrmEdit : UIEditForm
    {
        
        public FrmEdit(int id = 0)
        {
            InitializeComponent();
            if (id <= 0)
                Text = "新增";
            else
            {
                Text = "修改";
                GetInfo(id);
            }
        }

        private SysPail parameter;

        public SysPail Parameter
        {
            get
            {
                if (parameter == null)
                {
                    parameter = new SysPail();
                }
                parameter.PailId = txtPailId.Value;
                parameter.PailName = txtPailName.Text.Trim();
                parameter.Threshold_Value = txtThreshold_Value.Value;
                parameter.Current_Value = txtCurrent_Value.Value;
                parameter.Threshold_Weihght = txtThreshold_Weihght.Value;
                parameter.Current_Weight = txtCurrent_Weight.Value;
                parameter.Disable = ckDisable.Checked ? 1 : 0;
                return parameter;
            }

            set
            {
                parameter = value;
                txtPailId.Value = value.PailId;
                txtPailName.Text = value.PailName;
                txtThreshold_Value.Value = value.Threshold_Value;
                txtCurrent_Value.Value = value.Current_Value;
                txtThreshold_Weihght.Value = value.Threshold_Weihght;
                txtCurrent_Weight.Value = value.Current_Weight;
                ckDisable.Checked = value.Disable == 1 ? true : false;
            }
        }

        public void GetInfo(int id)
        {
            if (id > 0)
                Parameter = SysPailServices.GetInfo(new SysPail { Id = id });
        }

        private void FrmEdit_ButtonOkClick(object sender, EventArgs e)
        {
            if (!CheckData()) return;
            if (txtPailId.Value <= 0)
            {
                ShowWarningDialog("桶序号错误，请重新设置");
                return;
            }
            if (SysPailServices.PailidIsrepeat(parameter == null ? 0 : parameter.Id, txtPailId.Value))
            {
                ShowWarningDialog("桶序号重复，请重新设置");
                return;
            }
            if (SysPailServices.PailNameIsrepeat(parameter == null ? 0 : parameter.Id, txtPailName.Text.Trim()))
            {
                ShowWarningDialog("桶名称重复，请重新设置");
                return;
            }
            if (SysPailServices.Edit(Parameter))
                this.Close();
        }

        private void FrmEdit_Load(object sender, EventArgs e)
        {
           
        }
    }
}
